package org.seedltd.upms.controller.detection;

import java.util.Date;

import org.seedltd.base.exception.BusinessException;
import org.seedltd.core.base.BaseController;
import org.seedltd.core.base.BasePage;
import org.seedltd.core.utils.Assert;
import org.seedltd.springsecurity.utils.UserUtils;
import org.seedltd.upms.model.detection.form.DetectionOrderRefundForm;
import org.seedltd.upms.model.detection.form.DetectionOrderSearchForm;
import org.seedltd.upms.model.detection.vo.DetectionOrderBindingSampleVO;
import org.seedltd.upms.model.detection.vo.DetectionOrderVO;
import org.seedltd.upms.service.detection.DetectionOrderLogService;
import org.seedltd.upms.service.detection.DetectionOrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import lombok.AllArgsConstructor;

/**
 *
 * 订单管理控制器
 * 
 * @author laizuan
 * @version 1.0
 * @since 2021/07/31
 */
@RestController
@AllArgsConstructor
@RequestMapping("/detectionOrder")
public class DetectionOrderController extends BaseController<DetectionOrderService> {

    private final DetectionOrderLogService detectionOrderLogService;

    /**
     * 获取详情
     * 
     * @param id
     *            订单管理主键
     * @return 订单管理
     */
    @GetMapping(value = "/get")
    public DetectionOrderVO get(Long id) {
        Assert.notNull(id, "参数不能为空");
        return baseService.findById(id);
    }

    /**
     * 列表
     * 
     * @param searchForm
     *            查询条件
     * @return 订单管理列表集合
     */
    @PostMapping(value = "/list")
    public BasePage<DetectionOrderVO> page(@RequestBody DetectionOrderSearchForm searchForm) {
        return baseService.page(searchForm);
    }

    /**
     * 退款
     * 
     * @param refund
     *            退款对象
     * @return
     */
    @PostMapping("/refund")
    @PreAuthorize("hasAuthority('detection:order:refund')")
    public boolean refund(@RequestBody @Validated DetectionOrderRefundForm refund) {
        return baseService.refund(refund);
    }

    /**
     * 通知售货机出货
     * 
     * @param orderId
     *            订单主键
     * @return
     */
    @PreAuthorize("hasAuthority('detection:order:export')")
    @GetMapping("/export/{orderId}")
    public boolean exportGoods(@PathVariable Long orderId) {
        try {
            return baseService.exportGoods(orderId, true);
        } catch (Exception e) {
            detectionOrderLogService.add(UserUtils.getNickName(), new Date(), orderId, "后端手动触发出货异常：" + e.getMessage());
            throw new BusinessException(e.getMessage());
        }
    }

    /**
     * 获取绑定信息
     *
     * @param codeNo
     *            二维码编号
     */
    @PreAuthorize("hasRole('CHECKORG')")
    @GetMapping(value = "/binding/{codeNo}")
    public DetectionOrderBindingSampleVO binding(@PathVariable String codeNo) {
        return baseService.getBindingInfo(codeNo);
    }
}